/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.main.configurator.scripts;

import it.gashale.jacolib.core.JacolibError;
import it.gashale.jacolib.main.Jacolib;
import it.gashale.jacolib.main.configurator.Configurator;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


public class LoggerConfigurator extends Configurator {

	public void configure(Jacolib jacolib) throws JacolibError {
		String pattern=getValue(new String[]{"Pattern"}, "");
		String level=getValue(new String[]{"Level"}, "");
		String file=getValue(new String[]{"File"}, "");
		if(!file.equals("")) {
			FileHandler fh;
			try {
				fh = new FileHandler(file);
			} catch (SecurityException e) {
				throw new JacolibError("Configuration Exception: "+e.getMessage());
			} catch (IOException e) {
				throw new JacolibError("Configuration Exception: "+e.getMessage());
			}
			fh.setFormatter(new SimpleFormatter());
			Logger.getLogger(pattern).addHandler(fh);
		}
		if(!level.equals("")) {
			Logger.getLogger(pattern).setLevel(getLogLevel(level));
		}
	}
	
	Level getLogLevel(String loglevel_s) {
		Level l=null;
		if(loglevel_s.equals("FINEST")) {
			l=Level.FINEST;
		} else if(loglevel_s.equals("FINER")) {
			l=Level.FINER;
		} else if(loglevel_s.equals("FINE")) {
			l=Level.FINE;
		} else if(loglevel_s.equals("OFF")) {
			l=Level.OFF;
		} else if(loglevel_s.equals("ALL")) {
			l=Level.ALL;
		} else if(loglevel_s.equals("SEVERE")) {
			l=Level.SEVERE;
		} else if(loglevel_s.equals("WARNING")) {
			l=Level.WARNING;
		}
		return l;
	}

}
